package com.project.platform.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.project.platform.dto.CurrentUserDTO;
import com.project.platform.dto.RetrievePasswordDTO;
import com.project.platform.dto.UpdatePasswordDTO;
import com.project.platform.entity.Admin;
import com.project.platform.entity.Elder;
import com.project.platform.exception.CustomException;
import com.project.platform.mapper.ElderMapper;
import com.project.platform.service.ElderService;
import com.project.platform.utils.CurrentUserThreadLocal;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.project.platform.vo.PageVO;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 
 */
@Service
public class ElderServiceImpl  implements ElderService {
    @Resource
    private ElderMapper elderMapper;

    @Value("${resetPassword}")
    private String resetPassword;
    
    @Override
    public PageVO<Elder> page(Map<String, Object> query, Integer pageNum, Integer pageSize) {
        PageVO<Elder> page = new PageVO();
        List<Elder> list = elderMapper.queryPage((pageNum - 1) * pageSize, pageSize, query);
        page.setList(list);
        page.setTotal(elderMapper.queryCount(query));
        return page;
    }

    private void check(Admin entity) {
        Elder elder = elderMapper.selectByUserName(entity.getUsername());
        if (elder != null && !Objects.equals(elder.getId(), entity.getId())) {
            throw new CustomException("用户名已存在");
        }
    }

    @Override
    public CurrentUserDTO login(String username, String password) {
        Elder elder = elderMapper.selectByUserName(username);
        if (elder == null || !elder.getPassword().equals(password)) {
            throw new CustomException("用户名或密码错误");
        }
        if (elder.getStatus().equals("禁用")) {
            throw new CustomException("用户已禁用");
        }
        CurrentUserDTO currentUserDTO = new CurrentUserDTO();
        BeanUtils.copyProperties(elder, currentUserDTO);
        return currentUserDTO;
    }

    @Override
    public void register(JSONObject data) {
        Elder elder = new Elder();
        elder.setUsername(data.getString("username"));
        elder.setNickname(data.getString("nickname"));
        elder.setAvatarUrl(data.getString("avatarUrl"));
        elder.setPassword(data.getString("password"));
        elder.setStatus("启用");
        insert(elder);
    }


    @Override
    public void updateCurrentUserInfo(CurrentUserDTO currentUserDTO) {
        Elder elder = elderMapper.selectById(currentUserDTO.getId());
        elder.setId(currentUserDTO.getId());
        elder.setNickname(currentUserDTO.getNickname());
        elder.setAvatarUrl(currentUserDTO.getAvatarUrl());
        elder.setTel(currentUserDTO.getTel());
        elder.setEmail(currentUserDTO.getEmail());
        elderMapper.updateById(elder);
    }

    @Override
    public void updateCurrentUserPassword(UpdatePasswordDTO updatePassword) {
        Elder elder = elderMapper.selectById(CurrentUserThreadLocal.getCurrentUser().getId());
        if (!elder.getPassword().equals(updatePassword.getOldPassword())) {
            throw new CustomException("旧密码不正确");
        }
        elder.setPassword(updatePassword.getNewPassword());
        elderMapper.updateById(elder);
    }

    @Override
    public void resetPassword(Integer id) {
        Elder elder = elderMapper.selectById(id);
        elder.setPassword(resetPassword);
        elderMapper.updateById(elder);
    }

    @Override
    public void retrievePassword(RetrievePasswordDTO retrievePasswordDTO) {
        Elder elder = elderMapper.selectByTel(retrievePasswordDTO.getTel());
        if (elder == null) {
            throw new CustomException("手机号不存在");
        }
        //TODO 校验验证码
        elder.setPassword(retrievePasswordDTO.getPassword());
        elderMapper.updateById(elder);
    }

    @Override
    public Elder selectById(Integer id) {
        Elder elder = elderMapper.selectById(id);
        return elder;
    }

    @Override
    public List<Elder> list() {
        return elderMapper.list();
    }
    @Override
    public void insert(Elder entity) {
        check(entity);
        elderMapper.insert(entity);
    }
    @Override
    public void updateById(Elder entity) {
        check(entity);
        elderMapper.updateById(entity);
    }
    private void check(Elder entity) {
        Elder elder = elderMapper.selectByUserName(entity.getUsername());
        if (elder != null && !Objects.equals(elder.getId(), entity.getId())) {
            throw new CustomException("用户名已存在");
        }
    }
    @Override
    public void removeByIds(List<Integer> ids) {
        elderMapper.removeByIds(ids);
    }

    @Override
    public Elder selectByFamilyId(Integer id) {
        return elderMapper.selectByFamilyId(id);
    }

    @Override
    public List<Elder> selectByBuildingId(Integer id) {
        return elderMapper.selectByBuildingId(id);
    }

    @Override
    public void bindFamily(Elder entity) {
        elderMapper.updateById(entity);
    }
}
